package com.future.cb.web.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.future.cb.common.bean.Result;
import com.future.cb.common.exception.AppException;
import com.future.cb.common.exception.AuthenException;

import lombok.extern.slf4j.Slf4j;

@ControllerAdvice
@Slf4j
/**
 * 统一异常处理类
 * 
 * @author Leon.Zhang
 *
 */
public class AppExceptionHandler {
	/**
	 * 未捕获的异常，状态码500
	 * @param e
	 * @return
	 */
	@ExceptionHandler(Throwable.class)
	@ResponseBody
	public Result handler(Throwable e) {
		String errorMsg = e.getMessage();
		log.error("异常", e);
		return Result.Error500(errorMsg);
	}

	/**
	 * 认证异常，转台吗401
	 * @param e
	 * @return
	 */
	@ExceptionHandler(AuthenException.class)
	@ResponseBody
	public Result handler(AuthenException e) {
		return Result.Error401(e.getMessage());
	}
	/**
	 * 业务异常,自定义状态码111
	 * @param e
	 * @return
	 */
	@ExceptionHandler(AppException.class)
	@ResponseBody
	public Result handler(AppException e) {
		return Result.error(e.getMessage(), 111);
	}
}
